package com.cskaoyan.service.impl;

import com.cskaoyan.controller.stage.WxSearchController;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.*;
import com.cskaoyan.model.vo.WxSearchIndexDefaultVO;
import com.cskaoyan.service.WxKeyWordService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WxKeyWordServiceImpl
 * @Description: 微信小程序searchService层实现类
 * @Author:YPS
 * @Date: 2022/5/8 22:21
 * @Version: 1.0
 **/
@Service
@Transactional
public class WxKeyWordServiceImpl implements WxKeyWordService {
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public WxSearchIndexDefaultVO getDefaultIndex() {
        String username = null;
        WxSearchIndexDefaultVO wxSearchIndexDefaultVO = new WxSearchIndexDefaultVO();
        try {
            Subject subject = SecurityUtils.getSubject();
            username = (String) subject.getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            KeywordExample keywordExample = new KeywordExample();
            KeywordExample.Criteria criteria = keywordExample.createCriteria();
            criteria.andDeletedEqualTo(false);

            List<Keyword> keywordList = keywordMapper.selectByExample(keywordExample);
            wxSearchIndexDefaultVO.setDefaultKeyword(keywordList.get(0));

            for (int i = 0; i < keywordList.size(); i++) {
                if (keywordList.get(i).getIsDefault() == true){
                    wxSearchIndexDefaultVO.setDefaultKeyword(keywordList.get(i));
                }
                if (keywordList.get(i).getIsHot() == false){
                    keywordList.remove(i);
                }
            }
            wxSearchIndexDefaultVO.setHotKeywordList(keywordList);
            List<Map<String, String>> historyList = new ArrayList<>();
            wxSearchIndexDefaultVO.setHistoryKeywordList(historyList);
            return wxSearchIndexDefaultVO;
        }
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<Keyword> keywordList = keywordMapper.selectByExample(keywordExample);
        wxSearchIndexDefaultVO.setDefaultKeyword(keywordList.get(0));

        for (int i = 0; i < keywordList.size(); i++) {
            if (keywordList.get(i).getIsDefault() == true){
                wxSearchIndexDefaultVO.setDefaultKeyword(keywordList.get(i));
            }
            if (keywordList.get(i).getIsHot() == false){
                keywordList.remove(i);
            }
        }
        wxSearchIndexDefaultVO.setHotKeywordList(keywordList);

        Integer userId = userMapper.selectUserId(username);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria searchHistoryExampleCriteria= searchHistoryExample.createCriteria();
        searchHistoryExampleCriteria.andDeletedEqualTo(false);
        searchHistoryExampleCriteria.andUserIdEqualTo(userId);
        List<SearchHistory> histories = searchHistoryMapper.selectByExample(searchHistoryExample);
        List<Map<String, String>> historyList = new ArrayList<>();
        for (int i = 0; i < histories.size(); i++) {
            Map<String, String> stringStringMap = new HashMap<>();
            stringStringMap.put("keyword", histories.get(i).getKeyword());
            historyList.add(i, stringStringMap);
        }
        wxSearchIndexDefaultVO.setHistoryKeywordList(historyList);
        return  wxSearchIndexDefaultVO;
    }

    @Override
    public List<String> getHistoryList(String keyword) {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andKeywordLike("%" + keyword + "%");
        List<SearchHistory> historyList = searchHistoryMapper.selectByExample(searchHistoryExample);
        List<String> stringList = new ArrayList<>();
        for (SearchHistory searchHistory : historyList) {
            stringList.add(searchHistory.getKeyword());
        }
        return stringList;
    }

    @Override
    public void deleteHistory(Integer userId) {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        searchHistoryMapper.updateByExampleSelective(searchHistory, searchHistoryExample);
    }
}
